import unittest
import sys, os
from os.path import join, dirname
location = join(dirname(sys.argv[0]), '..')

sys.path.insert(0, location)
import weakref
import tasks

from decorators import with_autorelease_pool

class MockCommand(object):
    def __init__(self, *args, **kargs):
        pass
    
    def install(self, port):
        self.catcher.add('--->  Fetching figlet')
        # self.catcher.add('--->  Attempting to fetch figlet222.tar.gz from ftp://ftp.figlet.org/pub/figlet/program/unix/')
        self.catcher.add('--->  Verifying checksum(s) for figlet')
        self.catcher.add('--->  Extracting figlet')
        self.catcher.add('--->  Applying patches to figlet')
        self.catcher.add('--->  Configuring figlet')
        self.catcher.add('--->  Building figlet with target all')
        self.catcher.add('--->  Staging figlet into destroot')
        self.catcher.add('--->  Packaging tgz archive for figlet 2.2.2_0')
        self.catcher.add('--->  Installing figlet 2.2.2_0')
        self.catcher.add('--->  Activating figlet 2.2.2_0')
        self.catcher.add('--->  Cleaning figlet')

class AttemptingException(Exception):
    pass

class AssertingPerformInstallation(tasks.PerformActionBase):
    calls = ['on_fetching'   ,
             'on_verifying'  ,
             'on_extracting' ,
             'on_applying'   ,
             'on_configuring',
             'on_building'   ,
             'on_staging'   ,
             'on_packaging'  ,
             'on_installing' ,
             'on_activating' ,
             'on_cleaning'   
             ]
             
    @with_autorelease_pool
    def __call__(self):
        self.port_command.install(self.port)
    
    def _res(self):
        if not (self.last_called in AssertingPerformInstallation.calls):
            raise AttemptingException(self.last_called + ' not admissible')
    
    def __getattr__(self, item):
        self.last_called = item
        return self._res
        

class TaskTest(unittest.TestCase):
    def testPerformInstallationBase(self):
        mc         = MockCommand()
        pi         = AssertingPerformInstallation('figlet', c=mc)
        mc.catcher = weakref.proxy(pi)
        try:
            pi()
        except:
            self.fail('')
    


if __name__ == '__main__':
    unittest.main()